//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SubPeriodsPricer.h"
using namespace Cephei::QL::Experimental::Coupons;
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (boost::shared_ptr<QuantLib::SubPeriodsPricer>& childNative, Object^ owner) : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
#ifdef HANDLE
	_phSubPeriodsPricer = NULL;
#endif
	_ppSubPeriodsPricer = &childNative;
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppSubPeriodsPricer));
}
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (QuantLib::SubPeriodsPricer& childNative, Object^ owner) : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
#ifdef HANDLE
	_phSubPeriodsPricer = NULL;
#endif
	_ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (&childNative);
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppSubPeriodsPricer));
    _SubPeriodsPricerOwner = owner;
    _FloatingRateCouponPricerOwner = owner;
}

Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (CSubPeriodsPricer^ copy) : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
#ifdef HANDLE
	_phSubPeriodsPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (copy->GetShared());
        _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppSubPeriodsPricer));
    }
}
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (System::Type^ t) : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
#ifdef HANDLE
	_phSubPeriodsPricer = NULL;
#endif
	if (!t->IsSubclassOf(CSubPeriodsPricer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (QuantLib::Handle<QuantLib::SubPeriodsPricer>& childNative, Object^ owner)  : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
	_phSubPeriodsPricer = &childNative;
	_ppSubPeriodsPricer = &static_cast<boost::shared_ptr<QuantLib::SubPeriodsPricer>>(childNative.currentLink());
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppSubPeriodsPricer));
    _SubPeriodsPricerOwner = owner;
}
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (QuantLib::Handle<QuantLib::SubPeriodsPricer> childNative)  : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
	_phSubPeriodsPricer = &childNative;
	_ppSubPeriodsPricer = &static_cast<boost::shared_ptr<QuantLib::SubPeriodsPricer>>(childNative.currentLink());
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppSubPeriodsPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CSubPeriodsPricer (QuantLib::SubPeriodsPricer childNative)  : CFloatingRateCouponPricer(CSubPeriodsPricer::typeid)
{
#ifdef HANDLE
	_phSubPeriodsPricer = NULL;
#endif
	_ppSubPeriodsPricer = new boost::shared_ptr<QuantLib::SubPeriodsPricer> (new QuantLib::SubPeriodsPricer (childNative));
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppSubPeriodsPricer));
}
#endif

Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::~CSubPeriodsPricer ()
{
    if (_ppSubPeriodsPricer != NULL)
    {
	    delete _ppSubPeriodsPricer;
        _ppSubPeriodsPricer = NULL;
    }
}
Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::!CSubPeriodsPricer ()
{
    if (_ppSubPeriodsPricer != NULL)
    {
	    delete _ppSubPeriodsPricer;
    }
}
QuantLib::SubPeriodsPricer& Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::GetReference ()
{
    if (_ppSubPeriodsPricer == NULL) throw gcnew NativeNullException ();
	return **_ppSubPeriodsPricer;
}
boost::shared_ptr<QuantLib::SubPeriodsPricer>& Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::GetShared ()
{
    if (_ppSubPeriodsPricer == NULL) throw gcnew NativeNullException ();
	return *_ppSubPeriodsPricer;
}
QuantLib::SubPeriodsPricer* Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::GetPointer ()
{
    if (_ppSubPeriodsPricer == NULL) throw gcnew NativeNullException ();
	return &**_ppSubPeriodsPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SubPeriodsPricer>& Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::GetHandle ()
{
	if (_phSubPeriodsPricer == NULL)
	{
		_phSubPeriodsPricer = new Handle<QuantLib::SubPeriodsPricer> (*_ppSubPeriodsPricer);
	}
	return *_phSubPeriodsPricer;
}
#endif
bool Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::HasNative () 
{
	return (_ppSubPeriodsPricer != NULL);
}

Double Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CapletPrice (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSubPeriodsPricer)->capletPrice ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::CapletRate (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSubPeriodsPricer)->capletRate ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::FloorletPrice (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSubPeriodsPricer)->floorletPrice ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::FloorletRate (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSubPeriodsPricer)->floorletRate ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Coupons::ISubPeriodsPricer^ Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::Initialize (Cephei::QL::Cashflows::IFloatingRateCoupon^ coupon)
{
    CFloatingRateCoupon^ _Ccoupon;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccoupon = safe_cast<CFloatingRateCoupon^> (coupon);
        _Ccoupon->Lock();
        QuantLib::FloatingRateCoupon& _coupon = static_cast<QuantLib::FloatingRateCoupon&> (_Ccoupon->GetReference ()); 
    	(*_ppSubPeriodsPricer)->initialize ( _coupon );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccoupon != nullptr) _Ccoupon->Unlock();
    }
}
Double Cephei::QL::Experimental::Coupons::CSubPeriodsPricer::SwapletRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSubPeriodsPricer)->swapletRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

